﻿#Include _lib\Extend.ahk
#Include _lib\RunCMD.ahk
#Include _lib\ShinsOverlayClass.ahk
#Include _lib\MouseEvent.ahk

#NoTrayIcon

w := 50, h := 28, x := a_screenwidth - w, y := a_screenheight - h

overlay := ShinsOverlayClass(x, y, w, h, 1, 0, 0,)
me := MouseEvent(overlay.hwnd)
me.Register(MouseEvent.Events.LBUTTON_DOWN, MoveWin)
me.Register(MouseEvent.Events.LBUTTON_DBLCLK, (*) => overlay.gui.Move(a_screenwidth - w, a_screenheight - h))
me.Register(MouseEvent.Events.RBUTTON_UP, (*) => ExitApp())

chargeRem := 100

Draw()
SetTimer Draw, 1e3 * 30

Esc:: ExitApp()

Draw(*) {
  UpdateBatteryChargeRemaining()
  overlay.BeginDraw()
  local i, j

  ; #region 边框 (4)
  overlay.FillRectangle(i := 0, j := 0, w, h, '0xFF1D2021')
  ; overlay.DrawRectangle(i += 1, j += 1, w - 1, h - 1, '0xff012e00')
  i += 1, j += 1
  overlay.DrawRectangle(i += 1, j += 1, w - 3, h - 3, '0xffc0e4f0')
  ; #endregion

  ; #region 电池电量 (45)
  local _h, _c
  _h := 14 * chargeRem / 100, _c := GetTheme(chargeRem)
  overlay.DrawRectangle(i += 4, j += 4, 10, 17, '0xffc0e4f0')
  overlay.FillRectangle(i += 1, j += 1 + 14 - _h, 7, _h, _c)
  overlay.DrawText(chargeRem, i += 8, 0, 14, _c, , 'aCenter aMid w32 h' h)
  ; #endregion

  overlay.EndDraw()
}


GetTheme(r) {
  switch {
    case r >= 70: return '0xc0e4f0'
    case r >= 50: return '0xfdc273'
    case r >= 30: return '0xff5d6a'
    default: return '0xff0000'
  }
}

UpdateBatteryChargeRemaining() {
  batteryCmd := 'powershell -Command "Get-CimInstance -ClassName Win32_Battery | Select-Object -ExpandProperty EstimatedChargeRemaining"'

  r := RunCMD(batteryCmd)
  global chargeRem := r
}

MoveWin(*) {
  CoordMode "Mouse", "Screen"
  MouseGetPos(&px, &py), overlay.gui.GetPos(&wx, &wy)
  dx := wx - px, dy := wy - py
  While GetKeyState("LButton", "P") {
    MouseGetPos(&nx, &ny)
    if nx = px && ny = py
      continue
    overlay.gui.Move(nx + dx, ny + dy)
  }
  overlay.gui.GetPos(&wx, &wy)
  overlay.gui.Move(Clamp(wx, 0, A_ScreenWidth - w), Clamp(wy, 0, A_ScreenHeight - h))
}

inBox(pos, x, y, w, h) => pos.x.between(x, x + w) && pos.y.between(y, y + h)